﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using BesAsm.Stormwater.HspfCore;
using BesAsm.Framework.Reporting;

namespace BesAsm.Stormwater.HspfResults
{
  public class CatchmentResultsCollection : KeyedCollection<DateTime, CatchmentResult>, ICatchment, IMessenger
  {
    private Catchment _catchment;

    public CatchmentResultsCollection(Catchment catchment)
    {
      _catchment = catchment;
    }

    protected override DateTime GetKeyForItem(CatchmentResult item)
    {
      return item.Date;
    }

    #region ICatchment Members

    public string Name
    {
      get { return _catchment.Name; }
    }

    public LandSegmentsCollection Segments
    {
      get { return _catchment.Segments; }
    }

    public double Area
    {
      get { return _catchment.Area; }
      set { _catchment.Area = value; }
    }

    public void AddSegment(SegmentType segmentType, int segmentNum, double acres)
    {
      _catchment.AddSegment(segmentType, segmentNum, acres);
    }

    #endregion

    public void WriteResults(string file)
    {
      StreamWriter writer = new StreamWriter(file);

      Dictionary<string, object> resultsArray = this[0].GetResultsDictionary();

      writer.WriteLine("Catchment : {0}", this.Name);
      writer.WriteLine("Total Area (acres): {0}", this.Area);

      writer.WriteLine(
        BuildCsvString(resultsArray.Count),
        resultsArray.Keys.ToArray());

      foreach (CatchmentResult crc in this)
      {
        resultsArray = crc.GetResultsDictionary();
        writer.WriteLine(
          BuildCsvString(resultsArray.Count),
          resultsArray.Values.ToArray());
      }
      writer.Close();
      ReportMessage("Wrote .csv file '" + file + "'", ReportableMessageType.Info);
    }

    public string BuildCsvString(int count)
    {
      string s = string.Empty;

      for (int i = 0; i < count; i++)
        s += "{" + i + ":F6},";

      s = s.TrimEnd(new char[] { ',' });
      return s;
    }

    #region IMessenger Members

    public event MessageReportedEventHandler MessageReported;

    /// <summary>
    /// Called when a message is to be reported.
    /// </summary>
    /// <param name="e">The message to report</param>
    protected void OnMessageReported(MessageReportedArgs e)
    {
      if (MessageReported != null)
        MessageReported(this, e);
    }

    protected void ReportMessage(string message, ReportableMessageType type)
    {
      ReportableMessage rm = new ReportableMessage()
      {
        MessageType = type,
        Description = message
      };
      OnMessageReported(new MessageReportedArgs(rm));
    }

    #endregion

    public void AccumulateResult(CatchmentResult cr)
    {
      CatchmentResult thisCr = null;
      if (!this.Contains(cr.Date))
      {
        thisCr = new CatchmentResult(cr.Date);
        this.Add(thisCr);
      }
      else
        thisCr = this[cr.Date];

      thisCr.Agwo += cr.Agwo;
      thisCr.Ifwo += cr.Ifwo;
      thisCr.Igwi += cr.Igwi;
      thisCr.Pero += cr.Pero;
      thisCr.Supy += cr.Supy;
      thisCr.Suro += cr.Suro;      
    }
  }
}
